package com.spacing.app;

import java.util.*;

import com.senseilearning.spanish.R;
import com.spacing.app.data.Word;

/*
 * @author Tom
 * 
 * Nieuwe start
 */

public class Spacer2 {
	
	/*
	 * Global variables
	 */
	ArrayList<Word> words; //Word list

	double last_word_seen = -1;
	int nr_of_words_shown = 0;
	boolean last_word_correct = false;
	boolean first_session = true;
	int [] nActivations; //number of activations per index (word)
	int [] nActivationsOlderSessions;
	
	double [][] arrAlpha;
	double [] arrAlphaOlderSessions;
	double [][] arrActivationTimeWordOrderedWithOlderSessions;
	double [][] arrDecayValuesRecentAlpha;
	
	//time variable
	long session_start_time;
	long event_start_time;
	long psychological_time = 0;
	
	
	//model variables
	double standard_alpha = 0.32;
	double threshold = -0.8;
	double c = 0.25;
	
	
	public Spacer2(ArrayList<Word> words, boolean firstSession) {
		// set standard alpha
		// initialize the variables
		initVariables(words); //load the wordlist
		//TODO incorporate old results
	}
	
	private void initVariables (ArrayList<Word> words) {
		this.words = words;
		nActivations = new int[words.size()];
		nActivationsOlderSessions = new int[words.size()];
		session_start_time = System.currentTimeMillis();
		event_start_time = session_start_time;
		arrAlpha = new double [words.size()][];
		arrAlphaOlderSessions = new double [words.size()];
		arrActivationTimeWordOrderedWithOlderSessions = new double [words.size()][];
		arrDecayValuesRecentAlpha = new double [words.size()][];
	}
	
	public int determineNextPair () {
		double lowest_activation = Double.MAX_VALUE;
		long time;
		int look_ahead = 15;
		int temp_index = 0;
		int next = 0;
		double temp_alpha;
		double activation_value;
	
		time = (System.currentTimeMillis() - session_start_time+psychological_time+look_ahead * 1000) / 1000;
		double [] arr_temp_act_value = new double[words.size()];
		
		for(int i = 0; i < words.size(); i++) {
			if((nActivations[i] + nActivationsOlderSessions[i]) > 0) { //word is already seen
				if(nActivations[i] > 0) {
					temp_alpha = arrAlpha[i][(nActivations[i] - 1)];
				}else{
					temp_alpha = arrAlphaOlderSessions[i];
				}
				
				if((last_word_seen == i) && (last_word_correct) ) {//if last time already shown and correct
					activation_value = 100;
				}else{
					activation_value = getSpacingActivation(i, time, temp_alpha);
					arr_temp_act_value[i] = activation_value;
				}
				if(activation_value < lowest_activation) {
					lowest_activation = activation_value;
					temp_index= i;
				}
				
			}
		}
		
		if(lowest_activation < threshold) {
			next = temp_index;
		}else if(words.size() == nr_of_words_shown) { //all words are shown at least once
		//find the chunk with the oldest repetition
			
		}else{
			//show a new pair
			if(first_session) {
				next = nr_of_words_shown;
			}else{
				for(int i = 0; i < words.size(); i++) {
					if((nActivations[i]+nActivationsOlderSessions[i])==0) { //find the first unseen word.
						next = i;
						return next;
					}
				}
			}
		}
		
		return next;
	}

	private double getSpacingActivation(int i, long time, double temp_alpha) {
		double activation = 0;
		double sum = 0;
		
		double [] encounters = new double[nActivations[i]+nActivationsOlderSessions[i]];
		
		for(int j =0; j < (nActivations[i]+nActivationsOlderSessions[i]); j++) {
			encounters[j] = (arrActivationTimeWordOrderedWithOlderSessions[i][j]/1000);
			sum = sum + Math.pow((time-encounters[j]), arrDecayValuesRecentAlpha[i][j]);
		}
		activation = Math.log(sum);
			
		return activation;
	}
	
	public void updateAnswer(boolean is_correct, long answer_timestamp) {
		if (is_correct) {
			
		}
		
	}
}
